local BaseWnd = require(SCRIPTS_DIR .. ".wnds.BaseWnd");
local LoginWnd = class("LoginWnd",BaseWnd);
local gameState = require("framework.cc.utils.GameState");
function LoginWnd:ctor()
	LoginWnd.super.ctor(self);
    self.nameEditBox = nil;
    self.passEditBox = nil;

    self.uid = 0
    self.session = 0
    self.name = ""
    self.passwd = ""
end



function LoginWnd:onOpen()
	local wndName = self.wndData.name;
    self:setEnterGame();
    self:nameText();
    self:passText();
    --mgrs.proto:connect("115.159.122.80", 9100);
    --mgrs.proto:connect("192.168.1.134", 9100);
    self:initMail()
end

-- function LoginWnd:initMail()
--     mgrs.gameState:init(enums.Config.Mail)
--     mgrs.data:getPlayerData():initMail()
-- end

function LoginWnd:initMail()
    local initSucess = false
    local path = device.writablePath .. "/mail.lua"
    local exit = cc.FileUtils:getInstance():isFileExist(path)
    if exit == false then
        local str = "local mail={\r\n}\r\n return mail"
        local file = io.open(path, "wb")
        if file then
            local hr,err = file:write(str)
            if hr == nil then
                mgrs.log:printDebug("initMail write file error")
            else
                io.close(file)
                initSucess = true
            end
        else
            mgrs.log:printDebug("initMail open file error")
        end
    else
        initSucess = true
    end

    if initSucess == true then
        local oldmail = require("mail")
        mgrs.data:getPlayerData():parseMail(oldmail)
    end
end


function LoginWnd:setEnterGame()
    
    local enterGame = self:widget("middle.loginBt");
    self:createClick({targetNode = enterGame}):clickHandler(function()
        print("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX")
        mgrs.http:get({entry="login",api = "address",params={},cbk = function(resp)
            local data = json.decode(resp);
            dump(data)
            if data.ret == 1000 then
                mgrs.proto:connect(data.ip, data.port);
            else
                mgrs.hint:showHint(data.ret .. data.err)
                mgrs.hint:startShowHint();
            end
        end})
    end);
    
    self:addEventListener(enums.Event.SocketOpen,function(d)
        if d.data == enums.Socket.Login then
            self.name = self.nameEditBox:getText();
            self.passwd = self.passEditBox:getText();
            if self.name == '' or self.passwd == '' then
                -- body
                --mgrs.hint:showHint("用户名或密码不能为空")
                --mgrs.hint:startShowHint();
                mgrs.data:getLoginData():sendLogin('123456','123456')
            else
                mgrs.data:getLoginData():sendLogin(self.name,self.passwd)
                --mgrs.data:getLoginData():sendLoginWX()
                --self:login()
                --mgrs.data:getLoginData():sendTest()
            end
        end
    end)
end



function LoginWnd:nameText()
    local nameText = self:widget("middle.name");
    local psx,psy = nameText:getPosition();
    local editBox = cc.ui.UIInput.new({
        image = "png/1X1.png",
        size = nameText:getContentSize(),
        x = psx,
        y = psy,
        listener = function(event, editbox)
            if event == "began" then
            elseif event == "ended" then
            elseif event == "return" then
            elseif event == "changed" then
            else
            end
        end
    })
    editBox:setReturnType(cc.KEYBOARD_RETURNTYPE_SEND)
    nameText:getParent():addChild(editBox)
    self.nameEditBox = editBox;
    dump(self.nameEditBox)
end

function LoginWnd:passText()
    local passText = self:widget("middle.pass");
    local psx,psy = passText:getPosition();
    local editBox = cc.ui.UIInput.new({
        image = "png/1X1.png",
        size = passText:getContentSize(),
        x = psx,
        y = psy,
        listener = function(event, editbox)
            if event == "began" then
            elseif event == "ended" then
            elseif event == "return" then
            elseif event == "changed" then
            else
            end
        end
    })
    editBox:setReturnType(cc.KEYBOARD_RETURNTYPE_SEND)
    passText:getParent():addChild(editBox)
    self.passEditBox = editBox;
end



function LoginWnd:reqData(handler)
	handler(true);
end

--[[
function LoginWnd:getRelativePosition()
	return {rx = 0.5; ry = 0.5; ox = mgrs.wnd.visibleRect.center.x; oy = mgrs.wnd.visibleRect.center.y;	};
end
--]]

function LoginWnd:getLayer()
	return enums.Layer.Main;
end

function LoginWnd:getPreloadResList()
	return {};
end

function LoginWnd:getExclusiveType()
	return 0;
end


function LoginWnd:isOpenCloseSoundEnabled()
	return false;
end	


function LoginWnd:onClose()
    mgrs.log:printFuncCall();
end

function LoginWnd:onEnter()
	mgrs.log:printFuncCall();
end
function LoginWnd:onExit()
	mgrs.log:printFuncCall();
end
function LoginWnd:onEnterTransitionFinish()
	mgrs.log:printFuncCall();
end

function LoginWnd:onExitTransitionStart()
	mgrs.log:printFuncCall();
end

function LoginWnd:onCleanup()
	mgrs.log:printFuncCall();
    LoginWnd.super.onCleanup(self);
end

mgrs.wnd:registerWnd(enums.Wnd.Login,LoginWnd);
